window.project = {};

const vr = new VueRouter({
	routes: [{
			path: "/",
			component: {
				template: home.innerHTML,
				computed: {
					cookies() {
						const result = {};
						for (let s of document.cookie.split(";")) {
							if (s) {
								result[s.split("=")[0].trim()] = s.split("=")[1];
							}
						}
						setTimeout(() => {
							this.a++;
						}, 500);
						return result;
					},
					user() {
						return this.cookies["htqw_share_user"] ? JSON.parse(decodeURIComponent(this.cookies[
								"htqw_share_user"])) :
							false;
					}
				},
				data() {
					return {
						keyword: "",
						projects: []
					}
				},
				methods: {
					search() {
						document.cookie = "htqw_share_search_keyword=" + this.keyword;
						request("./ajax/get_projects.php", "json", (data, dialog) => {
							dialog.title("成功");
							dialog.content("请求成功，1秒后关闭此弹框");
							var projects = Object.values(data);
							projects.reverse();
							this.projects = projects;
							setTimeout(() => {
								dialog.close();
							}, 1000);
						}, (reason, dialog) => {
							dialog.title("失败");
							dialog.content(reason);
						});
					},
					detail(id) {
						this.$router.push("/detail?id=" + id);
					}
				},
				mounted() {
					this.search();
				}
			},
			name: "主页"
		},
		{
			name: "关于",
			path: "/about",
			component: {
				template: about.innerHTML
			}
		},
		{
			path: "/upload",
			component: {
				template: document.querySelector("template#upload").innerHTML,
				data() {
					return {
						name: "",
						describe: "",
						code: undefined,
						pic: undefined
					}
				},
				computed: {
					cookies() {
						const result = {};
						for (let s of document.cookie.split(";")) {
							if (s) {
								result[s.split("=")[0].trim()] = s.split("=")[1];
							}
						}
						setTimeout(() => {
							this.a++;
						}, 500);
						return result;
					},
					user() {
						return this.cookies["htqw_share_user"] ? JSON.parse(decodeURIComponent(this.cookies[
								"htqw_share_user"])) :
							false;
					}
				},
				methods: {
					picChange(e) {
						const file = e.target.files[0];
						const reader = new FileReader();
						const that = this;

						const size = file.size / 1048576
						if (size > 1) {
							const dg = dialog({
								title: "错误",
								content: "文件过大！"
							});
							dg.showModal();
						} else {

							reader.onload = function(event) {
								const dataURL = event.target.result;
								const mime = dataURL.split(',')[0].match(/:([^;]+);/)[1];
								if (mime === "image/png") {
									that.pic = dataURL;
								} else {
									const dg = dialog({
										title: "错误",
										content: "请选择png图片！"
									});
									dg.showModal();
								}
							};

							reader.readAsDataURL(file);
						}
					},
					codeChange(e) {
						const file = e.target.files[0];
						const reader = new FileReader();
						const that = this;
						const size = file.size / 1048576
						if (size > 5) {
							const dg = dialog({
								title: "错误",
								content: "文件过大！"
							});
							dg.showModal();
						} else {
							reader.onload = function(event) {
								const dataURL = event.target.result;
								const mime = dataURL.split(',')[0].match(/:([^;]+);/)[1];
								if ((mime === "application/x-zip-compressed") || (mime ===
										"application/zip")) {
									that.code = dataURL;
								} else {
									const dg = dialog({
										title: "错误",
										content: "请选择zip压缩包！"
									});
									dg.showModal();
								}
							};

							reader.readAsDataURL(file);
						}
					},
					submit() {
						const that = this;
						if (!(this.name && this.describe && this.pic && this.code)) {
							const dg = dialog({
								title: "错误",
								content: "信息未填全！"
							});
							dg.showModal();
						} else {
							document.cookie = "htqw_share_upload_name=" + this.name;
							document.cookie = "htqw_share_upload_discribe=" + this.describe;

							function requesta() {
								dg = dialog({
									title: "加载中"
								});
								dg.showModal();
								const fetchObject = fetch("./ajax/upload.php", {
									method: "POST",
									headers: {
										"Content-Type": "application/json;charset=utf8"
									},
									body: JSON.stringify({
										user: that.user,
										name: that.name,
										describe: that.describe,
										pic:that.pic,
										code:that.code
									})
								});
								fetchObject.catch(r => {
									
								});
								const t = fetchObject.then(r => r.json());
								t.then(c => {
									if(c.code == "errot") {
										dg.title("错误");
										dg.content(c.msg);
									}
									else {
										dg.title("成功");
										dg.content("即将返回主页");
										setTimeout(()=> {
											location.href="./"
										},1000);
									}
								});
								t.catch((r => {
									dg.title("请求错误");
									dg.content(r);
								}))
							}
							
							requesta()
						}

					}
				},
				mounted() {
					if (!this.user) {
						this.$route.push("/login");
					}
				}
			},
			name: "提交代码"
		},
		{
			path: "/detail",
			component: {
				template: detail.innerHTML,
				data() {
					return {
						isLoad: true,
						project: {}
					}
				},
				mounted() {
					request("./ajax/get_projects.php", "json", (data, dialog) => {
						this.project = data["p"+this.$route.query.id];
						dialog.close();
					}, (reason, dialog) => {
						dialog.title("失败");
						dialog.content(reason);
					});
				}
			},
			name: "项目详情"
		},
		{
			path: "/login",
			component: {
				template: login.innerHTML,
				data() {
					return {
						pass: true,
						id: "",
						password: ""
					}
				},
				methods: {
					login() {
						document.cookie = "htqw_share_login_password=" + this.password;
						document.cookie = "htqw_share_login_id=" + this.id;
						request("./ajax/login.php", "json", (c, dia) => {
							if (c.code === "success") {
								dia.title("成功");
								dia.content("即将返回主页");
								setTimeout(() => {
									dia.close();
									location.href = "./";
								}, 2000);
							} else {
								dia.title("失败");
								dia.content(c.message);
							}
						}, (r, dia) => {
							dia.title("请求失败");
							dia.content(r);
						});
					}
				}
			},
			name: "登录"
		},
		{
			path: "/user_home",
			component: {
				template: user_home.innerHTML,
				data() {
					return {
						user: false,
						a: 1,
						window: window
					}
				},
				computed: {
					cookies() {
						const result = {};
						for (let s of document.cookie.split(";")) {
							if (s) {
								result[s.split("=")[0].trim()] = s.split("=")[1];
							}
						}
						setTimeout(() => {
							this.a++;
						}, 500);
						return result;
					}
				},
				mounted: async function() {
					const cous = this.cookies["htqw_share_user"] ? JSON.parse(decodeURIComponent(this
							.cookies[
								"htqw_share_user"])) :
						false;
					this.user = cous;
				}
			},
			name: "用户主页"
		},
		{
			path: "/setup",
			component: {
				template: setup.innerHTML,
				data() {
					return {
						name: "",
						email: "",
						pass: true,
						code: "",
						password: ""
					}
				},
				methods: {
					getCode() {
						document.cookie = "htqw_share_setup_email=" + this.email;
						request("./ajax/setup_code.php", "json", (c, dia) => {
							if (c.code === "success") {
								dia.title("成功");
								dia.content("即将关闭本弹框");
								setTimeout(() => {
									dia.close();
								}, 1000);
							} else {
								dia.title("失败");
								dia.content(c.message);
							}
						}, (r, dia) => {
							dia.title("请求失败");
							dia.content(r);
						});
					},
					setupFn() {
						document.cookie = "htqw_share_setup_password=" + this.password;
						document.cookie = "htqw_share_setup_name=" + this.name;
						document.cookie = "htqw_share_setup_code=" + this.code;
						request("./ajax/setup.php", "json", (c, dia) => {
							if (c.code === "success") {
								dia.title("成功");
								dia.content("即将返回主页");
								setTimeout(() => {
									dia.close();
									location.href = "./";
								}, 1000);
							} else {
								dia.title("失败");
								dia.content(c.message);
							}
						}, (r, dia) => {
							dia.title("请求失败");
							dia.content(r);
						});
					}
				}
			},
			name: "注册"
		}
	]
});
vr.afterEach(to => {
	document.title = "htqw共享 | " + to.name
})
window.vm = new Vue({
	el: "#app",
	router: vr,
	data: {
		a: 0
	},
	computed: {
		cookies() {
			const result = {};
			for (let s of document.cookie.split(";")) {
				if (s) {
					result[s.split("=")[0].trim()] = s.split("=")[1];
				}
			}
			setTimeout(() => {
				this.a++;
			}, 500);
			return result;
		},
		user() {
			return this.cookies["htqw_share_user"] ? JSON.parse(decodeURIComponent(this.cookies[
					"htqw_share_user"])) :
				false;
		}
	},
	methods: {
		goHome() {
			if (this.user) {
				this.$router.push("/user_home");
			} else {
				this.$router.push("/login");
			}
		}
	}
});